package com.fenxiangle.yueding.feature.mine.view;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fenxiangle.yueding.R;
import com.fenxiangle.yueding.entity.bo.FoucsListBo;
import com.fenxiangle.yueding.entity.bo.OrderBo;
import com.fenxiangle.yueding.entity.bo.PublishDemandBo;
import com.fenxiangle.yueding.feature.focus.view.DemandManageActivity;
import com.fenxiangle.yueding.feature.focus.view.adapter.FocusAdapter;
import com.fenxiangle.yueding.feature.home.view.UserInfoActivity;
import com.fenxiangle.yueding.feature.mine.view.adapter.PublishDemandAdapter;
import com.fenxiangle.yueding.feature.publish.contract.PublishContract;
import com.fenxiangle.yueding.feature.publish.dependencies.mine.DaggerPublishComponent;
import com.fenxiangle.yueding.feature.publish.dependencies.mine.PublishPresenterModule;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.suozhang.framework.framework.BaseFragment;
import com.suozhang.framework.widget.EmptyView;
import com.suozhang.framework.widget.SpacesItemDecoration;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class FragmentPublishDemand extends BaseFragment implements PublishContract.View, OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener, BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.OnItemChildClickListener {
    @BindView(R.id.rv_publish_demand)
    RecyclerView mRvPublishDemand;
    private PublishDemandAdapter mAdapter;

    @BindView(R.id.smartRefesh)
    SmartRefreshLayout mSmartRefesh;

    private EmptyView mEmptyView;
    private int pageIndex = 1;
    private int pageSize = 10;
    List<PublishDemandBo> mData;

    @Inject
    PublishContract.Presenter mPresenter;
    private CallBackValue callBackValue;

    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_publish_demand;
    }

    @Override
    protected void initInjector() {
        DaggerPublishComponent.builder().publishPresenterModule(new PublishPresenterModule(this)).build().inject(this);
    }

    /**
     * fragment与activity产生关联是  回调这个方法
     */
    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        //当前fragment从activity重写了回调接口  得到接口的实例化对象
        callBackValue = (CallBackValue) getActivity();
    }

    @Override
    public void showPublishSuccess(OrderBo s) {

    }

    @Override
    public void showError(String error) {
        showErrorMsg(error);
    }

    @Override
    public void showInsufficientBalance() {

    }

    @Override
    public void showPublishListSuccess(List<PublishDemandBo> workBoPageData) {
        mSmartRefesh.finishRefresh();
        mData = workBoPageData;
        setData(true, mData);
        mAdapter.setEnableLoadMore(true);
    }

    @Override
    public void showPublishListError(String error) {
        if (mSmartRefesh != null) mSmartRefesh.finishRefresh();
        mAdapter.setEmptyView(mEmptyView.getErrorView(error));
        mAdapter.setNewData(null);
        mAdapter.setEnableLoadMore(true);
        mSmartRefesh.finishRefresh();
    }

    @Override
    public void showPublishListMoreSuccess(List<PublishDemandBo> msg) {
        setData(false, msg);
    }

    @Override
    public void showPublishListMoreError(String msg) {
        mAdapter.loadMoreFail();
    }

    @Override
    public void showPublishListEmpty() {
        if (mSmartRefesh != null) mSmartRefesh.finishRefresh();
        mAdapter.setEmptyView(mEmptyView.getEmptyView());
        mAdapter.setNewData(null);
    }

    @Override
    public void showcancleOrderSuccess() {
        showMsg("取消成功！");
        mSmartRefesh.autoRefresh();
    }

    @Override
    public void showConfrimOrderSuccess() {

    }

    @Override
    public void showConfrimTxSuccess() {

    }

    @Override
    public void showWeiQunanOrderSuccess() {

    }

    public void initAdapter() {
        mSmartRefesh.setEnableLoadMore(false);
        mSmartRefesh.setOnRefreshListener(this);
        mEmptyView = new EmptyView(mRvPublishDemand);
        mRvPublishDemand.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new PublishDemandAdapter();
        mAdapter.bindToRecyclerView(mRvPublishDemand);
        mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        mAdapter.setOnLoadMoreListener(this, mRvPublishDemand);
        mAdapter.setOnItemClickListener(this);
        mAdapter.setOnItemChildClickListener(this);
    }

    @Override
    protected void initView() {
        initAdapter();
    }

    @Override
    protected void initData() {
        pageIndex = 1;
        mPresenter.getPublishDemandList(pageIndex, pageSize);
//        mPresenter.getPublishInviteList(pageIndex, pageSize, 0);
        callBackValue.selectCounts(true);
    }

    @Override
    protected void initEvent() {

    }

    private void setData(boolean isRefresh, List data) {
        pageIndex++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdapter.setNewData(data);
        } else {
            if (size > 0) {
                mAdapter.addData(data);
            }
        }
        if (size < pageSize) {
            //第一页如果不够一页就不显示没有更多数据布局
            mAdapter.loadMoreEnd(isRefresh);
        } else {
            mAdapter.loadMoreComplete();
        }
    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }


    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refresh();
    }

    @Override
    public void onLoadMoreRequested() {
        loadMore();
    }


    private void refresh() {
        mAdapter.setEnableLoadMore(false);
        initData();
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    private void loadMore() {
        mPresenter.getPublishDemandListMore(pageIndex, pageSize);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId()) {

            case R.id.iv_my_publish_head:
                Intent intent = new Intent(getActivity(), UserInfoActivity.class);
                intent.putExtra("id", mData.get(position).getOwnUserUid());
                startActivity(intent);
                break;
            case R.id.btn_my_publish_cancle:
                mPresenter.cancleInvitedOrder(mData.get(position).getOrderId(), "");
                break;
            case R.id.btn_publis_selected://选定
                Intent intent1 = new Intent(getActivity(), MyDemandManageActivity.class);
                intent1.putExtra("user_id", mData.get(position).getOwnUserUid());
                intent1.putExtra("orderId", mData.get(position).getOrderId());
                startActivity(intent1);
                break;
        }
    }

    //定义一个回调接口
    public interface CallBackValue {
        public void selectCounts(boolean isSelect);
    }


}
